﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
/// <summary>
/// Summary description for DataProvider
/// </summary>
public class DataProvider
{
    public String strCon = null;
    public DataProvider()
    {
        strCon = ConfigurationManager.ConnectionStrings["QLBenhVien"].ToString();
    }

    // phuong thuc Lay Du Lieu
    public DataTable ExecuteQuery(string sql)
    {
        SqlConnection con = new SqlConnection();

        //string path =  System.Web.HttpContext.Current.Server.MapPath("~/App_Data/tintuc.mdb");

        //System.Configuration.AppSettingsReader reader = new AppSettingsReader();

        //string temp = (string)reader.GetValue("CSDL", typeof(String)); 
        con.ConnectionString = strCon;
        con.Open();
        DataTable dt = new DataTable();
        SqlCommand command = con.CreateCommand();
        command.CommandText = sql;
        SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        adapter.SelectCommand = command;
        con.Close();
        return ds.Tables[0];

        
    }

    public object ExecuteScalar(string sql)
    {
        SqlConnection con = new SqlConnection();

        con.ConnectionString = strCon;
        con.Open();

        SqlCommand command = new SqlCommand();
        command.CommandText = sql;
        command.Connection = con;

        return command.ExecuteScalar();
    }

    // phuong thuc Cap nhat thay doi tren Du lieu
    // them , xoa, sua
    public bool ExecuteNonQuery(string sql)
    {
        SqlConnection con = new SqlConnection();

        con.ConnectionString = strCon;
        con.Open();

        SqlCommand command = new SqlCommand();
        command.CommandText = sql;
        command.Connection = con;
        
        try
        {
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            return false;
            throw;
        }
        

        con.Close();
        return true;
    }
}
